<?php
/**
 * SourceController.php
 *
 * 源码查看控制器.
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	Home
 * @package		Home_Controller
 * @author 		Leo
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Momoger_Controller_Public */
//require_once 'Momoger/Controller/Public.php';

/**
 * 源码查看控制器.
 *
 * @category	Home
 * @package		Home_Controller
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class SourceController extends Momoger_Controller_Public {

	/**
	 * 默认显示源码首页页面
	 *
	 * 使用缓存支持
	 *
	 * @return HTML
	 */
	public function indexAction() {

		$projectRoot = dirname(APPLICATION_PATH);

		//set cache key
		$cacheKey = 'homeSourceIndexCodeSourceFileLists';

		$sources = $this->cache()->get($cacheKey);
		if (!$sources) {
			$sources = $this->getFiles($projectRoot, array('cache', 'log', 'upload', 'tmp', 'Zend', 'images'), array('favicon.ico'));
			$this->cache()->set($cacheKey, $sources, array(), 86400); //cache 1 day
		}
		$this->view->files = $sources;
	}

	/**
	 * 高亮显示代码源文件
	 *
	 * @return HTML
	 */
	public function viewAction() {
		// close layout
		if (null !== ($layout = Zend_Layout::getMvcInstance())) {
            $layout->disableLayout();
        }

		$fileName = $this->getRequest()->getParam('file');
		$fileUri = dirname(APPLICATION_PATH) . DIRECTORY_SEPARATOR . $fileName;
		$fileContent = 'The file can not be allowed read!';

		if (is_file($fileUri) && is_readable($fileUri)) {
			$fileContent = highlight_file($fileUri, TRUE);
			if (preg_match("/ini$/", $fileName)) {
				$fileContent = preg_replace("/(dbname|username|password)([&nbsp;]*)=([&nbsp;]*)\"([^\"]+)\"/", "$1 = There is a secret! ^o^", $fileContent);
			}
		}

		$this->view->fileName = $fileName;
		$this->view->fileContent = $fileContent;
	}

	/**
	 * 循环读取文件列表, 过滤隐藏文件
	 *
	 * @param string $root_dir 文件根目录
	 * @param array $remove_dirs 需要过滤的目录
	 * @param array $remove_files 需要过滤的文件
	 * @return array
	 */
	private function getFiles($root_dir, $remove_dirs = array(), $remove_files = array()) {
		$result = array();
		if (is_dir($root_dir)) {
			if (($dh = opendir($root_dir))) {
		        while (($file = readdir($dh)) !== false) {
		        	if ($file != '.' && $file != '..' && '.' != substr($file, 0, 1)) {
		        		if ('dir' == filetype($root_dir . DIRECTORY_SEPARATOR . $file)) {
		        			if (!in_array($file, $remove_dirs)) {
								$result[$file] = $this->getFiles($root_dir . DIRECTORY_SEPARATOR . $file, $remove_dirs, $remove_files);
		        			}
		        		} else {
		        			if (!in_array($file, $remove_files)) {
		        				$result[] = $file;
		        			}
		        		}
		        	}
		        }
		        closedir($dh);
		    }
		}
		return $result;
	}
}